{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 通过Numpy来计算向量内积与对应元素相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=[ 1  4  9 16 25];a_1=[ 1  4  9 16 25];b=55\n"
     ]
    }
   ],
   "source": [
    "# 向量：numpy 中的array；numpy中 * 和 multiply 都是表示向量的对应元素相乘，dot是向量的内积\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "array_1 = np.array([1,2,3,4,5])\n",
    "\n",
    "a = array_1*array_1\n",
    "a_1 = np.multiply(array_1,array_1)\n",
    "b = np.dot(array_1,array_1)\n",
    "print(\"a={};a_1={};b={}\".format(a,a_1,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2 User-CF协同过滤算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defaultdict(set, {'key1': 'value1'})"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import defaultdict\n",
    "dic1 = defaultdict(set)\n",
    "\n",
    "dic1['key1']= 'value1'\n",
    "\n",
    "dic1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda3\\lib\\site-packages\\scipy\\spatial\\distance.py:698: RuntimeWarning: invalid value encountered in double_scalars\n",
      "  dist = 1.0 - uv / np.sqrt(uu * vv)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import scipy.spatial.distance as ssd\n",
    "# ssd.cosine(v1,v2)的值越接近于0，两个向量的相似度越高\n",
    "v1 = [0,0]\n",
    "v2 = [0,0]\n",
    "sim1 = ssd.cosine(v1,v2)\n",
    "sim1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v11 = [1,1]\n",
    "v12 = [-1,1]\n",
    "sim2 = ssd.cosine(v11,v12)\n",
    "sim2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v21 = [1,1]\n",
    "v22 = [1,1]\n",
    "sim3 = ssd.cosine(v21,v22)\n",
    "sim3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
